package com.example.vue_shop.components.role.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.vue_shop.components.rights.entity.Rights;
import com.example.vue_shop.components.rights.entity.TreeRights;
import com.example.vue_shop.components.rights.mapper.RightsMapper;
import com.example.vue_shop.components.rights.utils.RightsUtils;
import com.example.vue_shop.components.role.entity.Role;
import com.example.vue_shop.components.role.entity.TreeRole;
import com.example.vue_shop.components.role.mapper.RoleMapper;
import com.example.vue_shop.components.role.service.RoleService;
import com.example.vue_shop.components.userrole.entity.UserRole;
import com.example.vue_shop.components.userrole.mapper.UserRoleMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author lipengwei
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Resource(name = "roleMapper")
    private RoleMapper roleMapper;

    @Resource(name = "rightsMapper")
    private RightsMapper rightsMapper;

    @Resource(name = "userRoleMapper")
    private UserRoleMapper userRoleMapper;

    @Override
    public List<TreeRole> getRoleList(int pageNum, int pageSize) {
        Page<Role> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        Page<Role> page1 = roleMapper.selectPage(page, new QueryWrapper<>());

        List<Role> roleList = page1.getRecords();
        List<TreeRole> treeRoles = new ArrayList<>(roleList.size());

        for (Role role : roleList) {
            // 根据角色Id获取每个角色对应的权限
            List<Rights> rights = rightsMapper.getRightsListByRoleId(role.getId());
            // 把却色获取到的资源转到树权限
            List<TreeRights> treeRights = RightsUtils.getTreeRights(rights);

            List<Integer> allRightsId = roleMapper.getAllRightsId(role.getId());

            List<TreeRights> children = new ArrayList<>();
            allRightsId.forEach(rightsId -> treeRights.forEach(treeRights1 -> {
                if (rightsId == treeRights1.getId()) {
                    children.add(treeRights1);
                }
            }));

            TreeRole treeRole = new TreeRole();
            treeRole.setId(role.getId());
            treeRole.setRoleName(role.getRoleName());
            treeRole.setRoleDesc(role.getRoleDesc());
            treeRole.setChildren(children);
            treeRoles.add(treeRole);
        }
        return treeRoles;
    }

    @Override
    public List<TreeRights> delRoleRights(int roleId, int rightsId) {
        roleMapper.delRoleRights(roleId, rightsId);
        // 根据角色Id获取每个角色对应的权限
        List<Rights> rights = rightsMapper.getRightsListByRoleId(roleId);
        // 把却色获取到的资源转到树权限
        return RightsUtils.getTreeRights(rights);
    }

    @Override
    public void addRoleRights(int roleId, String rightsIdsJson) {
        JSONObject jsonObject = JSONObject.parseObject(rightsIdsJson);
        String rightsIds = jsonObject.getString("rightsIntIds");
        String[] ids = rightsIds.split(",");

        Arrays.stream(ids).forEach(rightsIntId -> {
            Map<String, Object> roleRights = roleMapper.getRoleRights(roleId,
                    Integer.parseInt(rightsIntId));
            if (roleRights == null) {
                roleMapper.addRoleRights(roleId, Integer.parseInt(rightsIntId));
            }
        });
    }

    @Override
    public void addRole(Role role) {
        roleMapper.insert(role);
    }

    @Override
    public List<Role> getAllRoleList() {
        return roleMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public void updateUserRole(int userId, int roleId) {
        UserRole userRole = userRoleMapper.getUserRoleByUserId(userId);
        if (userRole == null) {
            UserRole userRole1 = new UserRole();
            userRole1.setUserId(userId);
            userRole1.setRoleId(roleId);
            userRoleMapper.insertUserRole(userRole1);
        } else {
            userRole.setRoleId(roleId);
            userRoleMapper.updateUserRole(userRole);
        }

    }

}
